home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / wgtrun.zip / RUN2.C < prev    next >
C/C++ Source or Header  |  1995-02-21  |  5KB  |  239 lines

  1. #include <wgt5.h>
  2.  
  3. /* 
  4.  Run2.c
  5.  
  6.  Same as run1.c, but with shadows.
  7.  
  8. */
  9.  
  10. unsigned char shadowtable[256]; /* Shadow table */
  11.  
  12. color pal[256];         /* The palette used for every graphic image */
  13. block sprites[200];     /* Array of images for the running robot */
  14.  
  15. block background;       /* Holds the background scrolling image */
  16. block work;             /* Page for constructing each frame */
  17.  
  18.  
  19. /* A structure which holds the scrolling values for each horizontal line */
  20. typedef struct
  21.  {
  22.   int x;                /* Current x value, shifted by 8 */
  23.   int increment;        /* fixed point increment */
  24.  } line_scroll;
  25. line_scroll lines[80];  /* 80 scrolling lines along the ground */
  26.  
  27. /* A simple sprite structure */
  28. typedef struct
  29.  {
  30.   int x;
  31.   int y;
  32.   int anm;              /* Sprite number */
  33.  } sprite;
  34. sprite people[5];
  35.  
  36.  
  37. int backx = 0;          /* X value for the scrolling rocks */
  38. int backinc;            /* X increment for scrolling rocks */
  39.  
  40.  
  41.  
  42.  
  43. /* Loads the graphics files, and allocates buffers */
  44. void load_graphics (void)
  45. {
  46.  work = wallocblock (320, 200);
  47.  /* Allocate a work buffer */
  48.  
  49.  wloadsprites (pal, "run.spr", sprites, 0, 199);
  50.  background = wloadgif ("street.gif", pal);
  51.  wsetpalette (0, 255, pal);
  52. }
  53.  
  54.  
  55.  
  56. /* Frees the buffers and sprites */
  57. void free_graphics (void)
  58. {
  59.  wfreesprites (sprites, 0, 199);
  60.  wfreeblock (background);
  61.  wfreeblock (work);
  62. }
  63.  
  64.  
  65. /* Set up the initial scrolling values */
  66. void init_lines (void)
  67. {
  68. int i;
  69. int inc;
  70.  
  71.  inc = 128;     /* slowest scrolling speed (128/256 of a pixel */
  72.  
  73.  backx = 0;     /* rocks x value */
  74.  backinc = inc; /* rocks same speed as the ground */
  75.  
  76.  for (i = 0; i < 80; i++)
  77.   {
  78.    lines[i].x = 0;              /* clear out the x value */
  79.    lines[i].increment = inc;    /* set the scroll speed */
  80.    inc += 32;                   /* Make the next row move faster */
  81.   }
  82.  
  83. }
  84.  
  85.  
  86.  
  87. /* Construct the background image */
  88. void animate_lines (void)
  89. {
  90. block source1, source2;
  91. block dest1, dest2;
  92. block origsource, origdest;
  93. int i;
  94. int x;
  95.  
  96.  wcopyscreen (0, 0, 319, 51, background, 0, 0, work);
  97.  /* Draw the moon stationary */
  98.  
  99.  
  100.  /* Scroll the rocks */
  101.  backx += backinc;
  102.  if (backx >= 81920)   /* 81920 is 320 << 8 */
  103.      backx -= 81920;
  104.  
  105.  x = backx >> 8;
  106.  
  107.  /* Copy the rocks */
  108.  wcopyscreen (x, 52, 319, 119, background, 0, 52, work);
  109.  if (x > 0)
  110.    wcopyscreen (0, 52, x-1, 119, background, 320 - x, 52, work);
  111.  
  112.  
  113.  
  114.  origdest = abuf + 120 * 320;           /* First row to copy */
  115.  origsource = background + 120 * 320;   /* First row to copy */
  116.  
  117.  for (i = 0; i < 80; i++)
  118.   {
  119.    /* Scroll this line */
  120.    lines[i].x += lines[i].increment;
  121.    if (lines[i].x >= 81920)   /* 81920 is 320 << 8, wraps scroll around */
  122.       lines[i].x -= 81920;
  123.  
  124.    
  125.    x = lines[i].x >> 8;
  126.    /* Get the x coord */
  127.  
  128.    dest1 = origdest + i * 320;
  129.    dest2 = dest1 + (319 - x);
  130.    source1 = origsource + i * 320;
  131.    source2 = source1 + x;
  132.    
  133.    /* Copy the line in two steps */
  134.    memcpy (dest1, source2, 320 - x);
  135.    if (x > 0)
  136.     memcpy (dest2, source1, x + 1);
  137.    
  138.   }
  139.  
  140. }
  141.  
  142.  
  143.  
  144. /* Animates and displays the running man */
  145. void animate_man (void)
  146. {
  147.  people[0].anm++;
  148.  if (people[0].anm > 29)
  149.    people[0].anm = 0;
  150.  
  151.  wputblock_shade (people[0].x, 158, sprites[people[0].anm+30],
  152.           shadowtable, 1);
  153.  wputblock (people[0].x, people[0].y, sprites[people[0].anm], 1);
  154.  
  155. }
  156.  
  157.  
  158.  
  159.  
  160. void wcreate_shadow_table (color *palette)
  161. {
  162. float fr, fg, fb;
  163. long ir, ig, ib;
  164.  
  165. long absr, absg, absb;
  166.  
  167.  
  168. int r,g,b;
  169.  
  170. short col;
  171. short findcol;
  172.  
  173. unsigned long lowest;
  174. unsigned char bestfit;
  175. unsigned long coldif;
  176.  
  177.  for (col = 0; col < 256; col++)
  178.   {
  179.  
  180.    fr = (float)palette[col].r * (0.5);
  181.    fg = (float)palette[col].g * (0.5);
  182.    fb = (float)palette[col].b * (0.5);
  183.  
  184.    ir = fr;
  185.    ig = fg;
  186.    ib = fb;
  187.  
  188.    lowest = 655350;
  189.    for  (findcol = 0; findcol < 256; findcol++)
  190.     {
  191.       absr = abs ( (long)palette[findcol].r - ir);
  192.       absg = abs ( (long)palette[findcol].g - ig);
  193.       absb = abs ( (long)palette[findcol].b - ib);
  194.  
  195.       coldif = absr + absg + absb;
  196.       if  ((coldif < lowest) && (findcol != col))
  197.       {
  198.     lowest = coldif;
  199.     bestfit = findcol;
  200.       }
  201.     }
  202.    shadowtable[col] = bestfit;
  203.   }
  204.  
  205. }
  206.  
  207.  
  208.  
  209.  
  210. void main (void)
  211. {
  212.  
  213.  vga256 ();
  214.  
  215.  load_graphics ();
  216.  
  217.  init_lines ();
  218.  
  219.  /* Set the position of the running man */
  220.  people[0].x = 120;
  221.  people[0].y = 50;
  222.  people[0].anm = 0;
  223.  
  224.  wcreate_shadow_table (pal);
  225.  
  226.  do { 
  227.   wsetscreen (work);
  228.   animate_lines ();
  229.   animate_man ();   
  230.   wnormscreen ();
  231.   wretrace ();
  232.   wputblock (0, 0, work, 0);
  233.   } while (!kbhit ());
  234.  
  235.  free_graphics ();
  236.  wsetmode (3);
  237. }
  238.  
  239.